package net.wexoo.onlinedivelog;

import java.util.Locale;

import net.ftlines.wicketsource.WicketSource;
import net.wexoo.onlinedivelog.view.pages.content.home.Home;
import net.wexoo.onlinedivelog.view.util.AuthenticatedPage;
import net.wexoo.onlinedivelog.view.util.DiveLogSession;

import org.apache.wicket.Session;
import org.apache.wicket.authorization.strategies.page.SimplePageAuthorizationStrategy;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyApp extends WebApplication {

  @SuppressWarnings("unused")
  private static Logger log = LoggerFactory.getLogger(MyApp.class);

  @Override
  public Class<Home> getHomePage() {
    return Home.class;
  }

  @Override
  public Session newSession(Request request, Response response) {
    Session diveLogSession = new DiveLogSession(request);

    Locale requestedLocale = diveLogSession.getLocale();
    Locale supportedLocale;

    if (requestedLocale.getLanguage().equals("de"))
      supportedLocale = Locale.GERMAN;
    // } else if (requestedLocale.getLanguage().equals("es")) {
    // supportedLocale = Locale.SPANISH;
    else
      supportedLocale = Locale.ENGLISH;

    diveLogSession.setLocale(supportedLocale);
    return diveLogSession;
  }

  @Override
  protected void init() {
    super.init();

    getComponentInstantiationListeners().add(new SpringComponentInjector(this));

    WicketSource.configure(this);

    getSecuritySettings().setAuthorizationStrategy(
        new SimplePageAuthorizationStrategy(AuthenticatedPage.class, Home.class) {

          @Override
          protected boolean isAuthorized() {
            return ((DiveLogSession) Session.get()).getLoggedInPerson() != null;
          }
        });
  }
}